/**  
* @Title: ShiroUtils.java 
* @Description:
* @Copyright: Copyright (c) 2018
* @Company:http://www.sinocon.cn
* @author Administrator  
* @date 2018年7月30日  
* @version 1.0  
*/
package com.simlink.sns.util;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;

import com.simlink.sns.model.User;

/**
 * @Title: ShiroUtils
 * @Description:
 * @author:Administrator
 * @date 2018年7月30日
 */
public class ShiroUtils {
	public static Subject getSubjct() {
		return SecurityUtils.getSubject();
	}

	public static Session getSession() {
		return SecurityUtils.getSubject().getSession();
	}

	public static void logout() {
		getSubjct().logout();
	}

	public static User getUser() {
		return (User) getSubjct().getPrincipal();
	}

	public static void setUser(User user) {
		Subject subject = getSubjct();
		PrincipalCollection principalCollection = subject.getPrincipals();
		String realmName = principalCollection.getRealmNames().iterator().next();
		PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
		// 重新加载Principal
		subject.runAs(newPrincipalCollection);
	}

	public static Integer getUserId() {
		return getUser().getId();
	}

	public static String getLoginName() {
		return "";
	}

	public static String getIp() {
		return getSubjct().getSession().getHost();
	}

	public static String getSessionId() {
		return String.valueOf(getSubjct().getSession().getId());
	}
}
